package docbookarchivemanager.converter;

import docbookarchivemanager.StartupManager;
import docbookarchivemanager.utils.DocbookFile;
import docbookarchivemanager.utils.DomManager;
import docbookarchivemanager.utils.Stylesheet;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 *
 * @author Jakub Hrachovec
 */
public class ManifestCreator {

    /**
     * Creates manifest in a temporary folder that will be zipped and outputted
     * to user. It includes version, main XML file, and list of stylesheets user
     * can choose form during transformation.
     */
    public static void createManifest() {
        try {
            DocumentBuilderFactory domFactory
                    = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = domFactory.newDocumentBuilder();
            Document doc = builder.newDocument();

            Element root = doc.createElementNS("http://docbook-archive.sf.net/ns/manifest", "manifest");
            doc.setXmlStandalone(true);
            //root.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
            root.setAttribute("version", "1.0");
            doc.appendChild(root);

            Element contents = doc.createElementNS("http://docbook-archive.sf.net/ns/manifest", "contents");

            contents.setAttribute("path", getContentsFileName());
            root.appendChild(contents);
            if (Converter.getStylesheets().size() > 0) {
                Element stylesheets = doc.createElementNS("http://docbook-archive.sf.net/ns/manifest", "stylesheets");
                root.appendChild(stylesheets);
                for (Stylesheet stylesheet : Converter.getStylesheets()) {
                    Element temp = doc.createElementNS("http://docbook-archive.sf.net/ns/manifest", "stylesheet");
                    temp.setAttribute("name", stylesheet.getName());
                    temp.setAttribute("path", stylesheet.getPath());
                    temp.setAttribute("note", stylesheet.getType().getIdent());
                    if (Stylesheet.IsFormatObject.TRUE.equals(stylesheet.isFo()) || Stylesheet.IsFormatObject.FALSE.equals(stylesheet.isFo())) {
                        temp.setAttribute("fo", stylesheet.isFo().name().toLowerCase());
                    }

                    stylesheets.appendChild(temp);

                }
            }
            DomManager.writeDom(doc, StartupManager.getGlobalOutputFolder() + File.separator + Converter.MANIFEST_NAME);
            System.out.println("Manifest created.");

        } catch (ParserConfigurationException ex) {
            System.out.println(ex.getMessage());
        }

    }

    private static String getContentsFileName() {
        for (DocbookFile f : Converter.getListOfIncludedFilesToGoThrough()) {
            if (!f.IsIncluded()) {
                return new File(f.getAbsolutePath()).getName();
            }
        }
        return StartupManager.getGlobalNameOfFile();
    }
}
